package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.domain.role.RoleDO;
import com.qk.dto.role.RoleDTO;
import com.qk.entity.Role;
import com.qk.management.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author: Enid
 * @date: 2025/10/9 11:30
 * @description:
 */
@Slf4j
@RestController
@RequestMapping("/roles")
public class RoleController {
    @Autowired
    private RoleService roleService;
    /**
     * 新增角色
     */
    @PostMapping
    public Result add(@RequestBody Role role){
        log.info("新增角色：{}",role);
        roleService.add(role);
        return Result.success();
    }
    /**
     * 获取角色列表
     */
    @GetMapping
    public Result getList(RoleDTO roleDTO){
        log.info("获取角色列表，参数：角色名称={},角色标签={},当前页码={},每页大小={}",roleDTO.getName(),roleDTO.getLabel(),roleDTO.getPage(),roleDTO.getPageSize());
        PageResult<Role> pageResult = roleService.getList(roleDTO);
        return Result.success(pageResult);
    }
    /**
     * 根据id查询角色
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable("id") Integer id){
        log.info("根据id查询角色id为：{}",id);
        Role role = roleService.selectById(id);
        return Result.success(role);
    }
    /**
     * 修改角色
     */
    @PutMapping
    public Result update(@RequestBody RoleDO roleDO){
        log.info("修改角色：{}", roleDO);
        roleService.updateById(roleDO);
        return Result.success();
    }
    /**
     * 删除角色
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id){
        log.info("删除角色id为：{}",id);
        roleService.deleteById(id);
        return Result.success();
    }
    /**
     * 查询所有角色
     */
    @GetMapping("/list")
    public Result list(){
        return Result.success(roleService.list());
    }

}
